/*

 * Copyright (C) 2021 Huawei Device Co., Ltd.

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *     http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */
package com.aries.ui.widget.demo.slice;

 import com.aries.ui.helper.navigation.KeyboardHelper;
 import com.aries.ui.helper.navigation.NavigationViewHelper;
 import com.aries.ui.view.radius.RadiusEditText;
 import com.aries.ui.widget.demo.ResourceTable;
 import com.aries.ui.widget.demo.base.BaseAbilitySlice;
 import com.aries.ui.widget.demo.module.alert.AlertAbilitySlice;
 import com.aries.ui.widget.demo.util.SizeUtil;
 import ohos.aafwk.ability.Ability;
 import ohos.aafwk.content.Intent;
 import ohos.agp.colors.RgbPalette;
 import ohos.agp.components.Component;
 import ohos.agp.components.Text;
 import ohos.agp.text.Font;
 import ohos.agp.window.service.WindowManager;
 import ohos.multimodalinput.event.TouchEvent;

 /**
  *TitleEditAbtivity
  */
 public class TitleEditAbtivity extends BaseAbilitySlice implements KeyboardHelper.OnKeyboardVisibilityChangedListener {
    Text mTvTip, mtvTitleEditTip;
    RadiusEditText mEtMottomTitle;
    Component rootResId;
    protected NavigationViewHelper navigationViewHelper;


    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
        onStart(intent);
//        mEtMottomTitle.requestFocus();

    }

    @Override
    protected void setTitleBar() {
//        getWindow().addFlags(WindowManager.LayoutConfig.MARK_TRANSLUCENT_STATUS);
        getWindow().setInputPanelDisplayType(WindowManager.LayoutConfig.INPUT_ADJUST_PAN);
        titleBar.setTitleMainText("KeyboardHelper示例")
                .setStatusBackgroundColor(RgbPalette.WHITE.asArgbInt())
                .setRightTextDrawable(ResourceTable.Media_ic_menu)
                .setRightTextDrawableWidth(60)
                .setRightTextDrawableHeight(60)
                .setOnRightTextClickListener(component -> {
                    present(new AlertAbilitySlice(), new Intent());
                });

    }

    @Override
    protected void initView(Intent intent) {
        rootResId = findComponentById(ResourceTable.Id_rootResId);
        mTvTip = (Text) findComponentById(ResourceTable.Id_tv_tipTitleEdit);
        mtvTitleEditTip = (Text) findComponentById(ResourceTable.Id_tv_title_edit_tip);
        mEtMottomTitle = (RadiusEditText) findComponentById(ResourceTable.Id_et_bottomTitle);

        keyboardHelper.setEnable(ResourceTable.Id_et_bottomTitle)
                .setOnKeyboardVisibilityChangedListener(this::onKeyboardVisibilityChanged);
        rootResId.setTouchEventListener(new Component.TouchEventListener() {
            @Override
            public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
                keyboardHelper.closeKeyboard(mEtMottomTitle);
                return false;
            }
        });
    }

    @Override
    protected int getLayout() {
        return ResourceTable.Layout_ohos_edit_text;
    }

    @Override
    protected void beforeControlNavigation(NavigationViewHelper navigationHelper) {
        super.beforeControlNavigation(navigationHelper);
        navigationHelper.setOnKeyboardVisibilityChangedListener(this);
    }

    /**
     * 当前Ability 是否开启
     * @param activity 当前Ability
     * @param isOpen 软键盘是否开启
     * @param heightDiff 预留让软件盘上移高度--给contentView累加设置的paddingBottom
     * @param navigationHeight 当前导航栏高度
     * @return 当前Ability 是否开启
     */
    @Override
    public boolean onKeyboardVisibilityChanged(Ability activity, boolean isOpen, int heightDiff, int navigationHeight) {
        getUITaskDispatcher().asyncDispatch(() -> {
            mTvTip.setText("软键盘开启状态:" + isOpen + ";追加paddingBottom:" + heightDiff + ";虚拟导航栏高度:" + navigationHeight/*titleBar.getNavigationBarHeight()*/);
            mTvTip.setTextSize(12, Text.TextSizeType.FP);
            mTvTip.setFont(Font.DEFAULT_BOLD);
            int padding = SizeUtil.dp2px(12);
            mTvTip.setPadding(padding, padding, padding, 0);
        });
        return false;
    }
}
